var Blemash = require("../BleMash-js/blemash.js")
var Ajax = require("../../utils/ajax.js")
var app = getApp()
var Modal = require("../../utils/modal.js")
var Handle = require("../../utils/handle.js")
var System = require("../../utils/system.js")
Page({
  data:{
      showLightNanme:false,
      cold:255,
      warm:255,
      devices: [],
      pickeddevice: {},//被选中的灯,是devices中的一个
      type:0//是遥控器还是灯 0 是灯 1是遥控器
  },
  editlight: function (e) {
    var index= e.currentTarget.dataset.index;
    this.setData({
      pickeddevice:this.data.devices[index]
    });
    if (this.data.showLightNanme){
      this.setData({ showLightNanme:false});
    }else{
      this.setData({ showLightNanme: true});
      var pickeddevice = this.data.pickeddevice;
      if (pickeddevice.localName == "Air Control"){ 
        this.setData({ type:1 });
      }else{
        this.setData({ type:0 });
      }
    }
  },
  add_light: function (name) {
    Ajax.AsyncPost("/light/get_node_addr_in_mesh/", { meshid: app.globalData.activemesh.id }, (data) => {
      Handle.cancle = false;
      Handle.add(app.globalData.activemesh, this.data.pickeddevice.deviceId, data.device_address, res => {
        const newlight = {
          name: name, cold: 0, warm: 0, red: 0, green: 0, blue: 0,
          meshid: app.globalData.activemesh.id,
          uuid: res.uuid,
          deviceaddress: data.device_address,//是外层请求返回的
          devicekey: res.devicekey,//是addDevice返回的
          type: System.getSystem()
        };
        Ajax.AsyncPost("/light/add_light_into_mesh/", newlight, (data) => {
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2];
          newlight.id = data.light_id;
          prevPage.data.lights.push(newlight);
          prevPage.setData({lights: prevPage.data.lights });
          wx.navigateBack();
        })
      });
    })
  },
  add_con: function (name) {
    Ajax.AsyncPost("/light/get_node_addr_in_mesh/", { meshid: app.globalData.activemesh.id }, (data) => {
      Handle.cancle = false;
      Handle.add(app.globalData.activemesh, this.data.pickeddevice.deviceId, data.device_address, res => {
        const newcon = {
          name: name,
          m_id: app.globalData.activemesh.id,
          uuid: res.uuid,
          address: data.device_address,//是外层请求返回的
          devicekey: res.devicekey,//是addDevice返回的
          type: System.getSystem()
        };
        Ajax.AsyncPost("/con/add_con_into_mesh/", newcon, (data) => {
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2];
          newcon.c_id = data.c_id;
          prevPage.data.cons.push(newcon);
          prevPage.setData({cons: prevPage.data.cons});
          wx.navigateBack();
        })
      });
    })
  },
  formSubmit: function(e) {
    const name = e.detail.value.l_name;
    if (this.data.type == 0) this.add_light(name);
    else this.add_con(name);

  },
  onLoad() {
    wx.stopPullDownRefresh() //刷新完成后停止下拉刷新动效
    app.globalData.activemesh.provisioning_cancle = false;  //本页面中，允许添加动作
    this.setData({ devices: [] });
    app.globalData.activemesh.scan((value) => {
      this.data.devices.push(value);
      this.setData({ devices: this.data.devices });
    });
  },
  onUnload:function(e){
    Handle.cancle_add();
    app.globalData.activemesh.provisioning_cancle=true;  //跳出页面，本页面的添加动作取消
    app.globalData.activemesh.status = null;  //跳出页面，可以连接代理
    app.globalData.activemesh.stopScan();
  },
  onPullDownRefresh: function () {
    app.globalData.activemesh.stopScan((value)=>this.onLoad());
  },
})

